<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2016/9/8
  Time: 15:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <jsp:include page="../../head.jsp"/>
    <title>Title</title>
    <script type="application/javascript" src="../../js/gtimp3/declaration.js"></script>
    <script type="text/javascript">
        var formSetting = {
            title: "查询信息",
            module: 'query',
            columns: [
                {
                    name: "declaration_type",
                    label: "进出口类型",
                    type: "dropdown",
                    data: [["进口", "I"], ["出口", "E"]],
                    op: "eq",
                    minLength: 0
                }, {
                    name: "contract_no",
                    label: "合同协议号"
                }, {
                    name: "businessId",
                    label: "业务编号"
                }, {
                    name: "container_no",
                    label: "集装箱号"
                }, {
                    name: "company_co",
                    label: "收发货人",
                    type: "dropdown",
                    dropdownType: "ajax",
                    url: "../order-dropdown/company",
                    op: "eq",
                    minLength: 1,
                    keyWordName: "key",
                    success: function (data) {
                        data = data.results;
                        var array = new Array();
                        $.each(data, function (i, n) {
                            array.push([n["text"], n["value"]]);
                        });
                        return array;
                    }
                }, {
                    name: "bl_no",
                    label: "提运单号"
                }, {
                    name: "confirmTime",
                    label: "提交时间起",
                    type: "date",
                    op: "ge"
                }, {
                    name: "confirmTime",
                    label: "提交时间止",
                    type: "date",
                    op: "le"
                }, {
                    name: "declaration_port_code",
                    label: "申报口岸",
                    type: "dropdown",
                    dropdownType: "ajax",
                    url: "../order-dropdown/supCompany?isExport=" + true,
                    op: "eq",
                    minLength: 0,
                    keyWordName: "key",
                    success: function (data) {
                        data = data.results;
                        var array = new Array();
                        var temp = new Array();
                        $.each(data, function (i, n) {
                            if ($.inArray(n["data"]["port_code"], temp) == -1) {
                                array.push([n["data"]["port"], n["data"]["port_code"]]);
                                temp.push(n["data"]["port_code"]);
                            }
                        });
                        return array;
                    }
//                    data: [["高明海关", "5181"], ["肇庆三榕", "5177"], ["云浮新港", "5196"], ["佛山澜石", "5182"],
//                        ["南沙新港", "5166"], ["南海九江", "5112"], ["佛山新港", "5189"]],
//                    op: "eq",
//                    minLength: 0
                }, {
                    name: "creator",
                    label: "下单人"
                }, {
                    name: "supCompany.name",
                    label: "业务供应商"
                }],
            buttons: {
                custom: []
            },
            grid: {
                id: "declarationsDataGrid",
                title: "查询结果",
                url: "findPaidList",
                multiselect: true,
                colModel: [{
                    index: "ID",
                    name: "id",
                    hidden: true
                }, {
                    name: "evaluation.evalId",
                    label: "evaluation",
                    hidden: true
                }, {
                    index: "DECLARATION_TYPE",
                    name: "declaration_type",
                    hidden: true
                }, {
                    index: "ENTER_PAGE",
                    name: "enter_page",
                    hidden: true
                },
                    {
                        name: "businessId",
                        label: "业务编号",
                        formatter: function (cellvalue, options, rowObject) {
                            return "<a href=\"javascript:;\" onclick=\"javascript:doEdit('"
                                + rowObject.id + "','"
                                + rowObject.declarationType + "','"
                                + rowObject.enter_page + "');\">" + cellvalue
                                + "</a>";
                        }

                    }, {
                        name: "container_no",
                        label: "集装箱号"
                    }, {
                        name: "contract_no",
                        label: "合同协议号"
                    }, {
                        name: "company_name",
                        label: "收发货人"
                    }, {
                        name: "supCompany.name",
                        label: "业务供应商"
                    }, {
                        name: "declaration_port",
                        label: "申报口岸"
                    }, {
                        name: "confirmTime",
                        label: "业务提交时间"
                    }, {
                        name: "creator",
                        label: "下单人",
                        formatter: function (cellvalue, options, rowObject) {
                            return "<a href=\"javascript:;\" onclick=\"javascript:showContactWay('"
                                   + rowObject.id + "','creator');\">" + cellvalue + "</a>";
                        }
                    }, {
                        name: "validator.name",
                        label: "审核人",
                        formatter: function (cellvalue, options, rowObject) {
                            if(cellvalue){
                                return "<a href=\"javascript:;\" onclick=\"javascript:showContactWay('"
                                       + rowObject.id + "','validator');\">" + cellvalue + "</a>";
                            }else{
                                return "";
                            }
                        }
                    }, {
                        name: "diDans",
                        label: "底单下载",
                        formatter: function (cellvalue, options, rowObject) {
                            //alert(cellvalue);
                            if (cellvalue.length > 0) {
                                return "<a href=\"javascript:;\" onclick=\"javascript:jw.dialog({iframe : 'uploadMain?type=didan&no-upload=1&hide=1#"
                                    + rowObject.id
                                    + "',title : '底单下载',iframeFetchTitle : false,width : 815,height : 620});\">下载</a>";
                            }
                            else {
                                return "<a style='color:red;' href=\"javascript:;\" onclick=\"javascript:dowmload('"
                                    + rowObject.id + "');\">下载</a>";
                            }
                            return "";
                        }
                    }, {
                        name: "sno",
                        label: "报关单号"
                    }, {
                        name: "state",
                        label: "业务状态",
                        formatter: function (cellvalue, options, rowObject) {
                            switch (cellvalue) {
                                case "DS00":
                                    return "草稿状态";
                                case "DS01":
                                    return "待审核";
                                case "DS02":
                                    return "已审核";
                                case "DS03":
                                    return "驳回";
                                case "DS04":
                                    return "已审核";
                                case "DS05":
                                    return "放行";
                                case "DS06":
                                    return "已审核";
                                case "DS07":
                                    return "已审核";
                                case "DS08":
                                    return "已审核";
                                case "DS09":
                                    return "查验";
                                case "DS10":
                                    return "海关放行";
                                case "DS11":
                                    return "商检放行";
                                case "DS13":
                                    return "海关退单";
                                case "DS120":
                                    return "查验中";
                                case "DS121":
                                    return "查验完成";
                                case "DS122":
                                    return "查验不通过";
                                case "DS124":
                                    return "海关放行";
                                case "DS125":
                                    return "商检放行";
                                case "DS126":
                                    return "其他放行";
                                default:
                                    return cellvalue;
                            }
                        }
                    }, {
                        label: "操作",
                        sortable: false,
                        buttons: [{
                            key: "id1",
                            value: "详情",
                            click: function (gridid, rowId, rowdata) {
                                doEdit(rowId, rowdata.declaration_type,rowdata.enter_page);
                            }
                        }, {
                            key: "id2",
                            value: "评价",
                            hide: function (rowdata) {
                                if (rowdata.state != 'DS05') {
                                    return true;
                                } else if (!rowdata.evaluation) {
                                    return false;
                                } else if (!rowdata.evaluation.evaluationRecords) {
                                    return false;
                                } else return rowdata.evaluation.evaluationRecords.length > 0;

                            },
                            click: function (gridid, rowId, rowdata) {
                                doEvaluate(rowId);
                            }
                        }, {
                            key: "id3",
                            value: "追评",
                            hide: function (rowdata) {
                                if (!rowdata.evaluation) {
                                    return true;
                                } else if (!rowdata.evaluation.evaluationRecords) {
                                    return true;
                                } else if (rowdata.evaluation.evaluationRecords.length == 2) {
                                    return false;
                                } else {
                                    return true;
                                }

                            },
                            click: function (gridid, rowId, rowdata) {
                                doEvaluate(rowId);
                            }
                        }, {
                            key: "id4",
                            value: "查看评价",
                            hide: function (rowdata) {
                                if (!rowdata.evaluation) {
                                    return true;
                                } else if (!rowdata.evaluation.evaluationRecords) {
                                    return true;
                                } else if (rowdata.evaluation.evaluationRecords.length > 0) {
                                    return false;
                                } else {
                                    return true;
                                }

                            },
                            click: function (gridid, rowId, rowdata) {
                                lookEvaluation(rowId);
                            }
                        }, {
                            key: "id5",
                            value: "海关退单信息",
                            hide: function (rowdata) {
                                if (rowdata.state != "DS13") {
                                    return true;
                                } else {
                                    return false;
                                }
                            },
                            click: function (gridId, id, rowData) {
                                var url = "/business/reject/customer/checkChargeback?declaId="
                                    + rowData.id;
                                jw.dialog({
                                    iframe: url,
                                    iframeFetchTitle: false,
                                    width: 500,
                                    height: 230
                                });
                            }
                        }],
                        formatter: $.jqGrid.custom.formatter.button
                    }],
                sortname: "businessId",
                sortorder: "desc",
                ondblClickRow: function (rowid, iRow, iCol, e) {
                    if (rowid) {
                        var rowData = $("#declarationsDataGrid").jqGrid("getRowData", rowid);
                        doEdit(rowid, rowData.declaration_type,declaration.enter_page);
                    }
                },
                gridComplete: function () {
                    markChargeback();
                    markCustomerReject();
                }
            }
        };

        function dowmload(bizId) {
            var data = "id=" + bizId;
            $.ajax({
                url: "declaration-dowmlaodDeclara",
                cache: false,
                dataType: "json",
                data: data,
                success: function (data) {
                    console.log(data);
                    if (data.result) {
                        window.location.href =
                            "declaration-downloadDeclarationFile?id=" + bizId;
                    } else {
                        alert(data.msg);
                    }
                }
            });
        }
        $(document).ready(function () {
            $("#declarationsDataTable").mrdForm(formSetting);
        });

        /**
         * 标记出海关退回的报关单
         */
        function markChargeback() {
            var ids = $("#declarationsDataGrid").jqGrid("getDataIDs");
            var rowDatas = $("#declarationsDataGrid").jqGrid("getRowData");
            for (var i = 0; i < rowDatas.length; i++) {
                var rowData = rowDatas[i];
                if ("海关退单" == rowData.state) {
                    $("#" + ids[i] + " td").css("background-color", "pink");
                }
            }
        }
        /**
         * 标记出客户正申请驳回的报关单
         */
        var rejectRecords;
        function markCustomerReject() {
            var ids = $("#declarationsDataGrid").jqGrid("getDataIDs");
            var rowDatas = $("#declarationsDataGrid").jqGrid("getRowData");
            var declarationIds = '';
            for (var i = 0; i < rowDatas.length; i++) {
                var rowData = rowDatas[i];
                declarationIds += rowData.id + ",";
            }
            $.ajax({
                url: "/business/reject/rejectRecord",
                cache: false,
                dataType: 'json',
                data: {"declarationIds": declarationIds},
                success: function (data) {
                    rejectRecords = data.data;
                    if (rejectRecords) {
                        for (var i = 0; i < rowDatas.length; i++) {
                            var rejectRecord = rejectRecords[i];
                            if (rejectRecord) {
                                if (rejectRecord.acceptedResult == 'INITIATED') {
                                    $("#" + ids[i] + " td").css("background-color", "pink");
                                    $("#declarationsDataGrid")
                                        .jqGrid('setCell', ids[i], 'applied', true);
                                    $("#declarationsDataGrid")
                                        .jqGrid('setCell', ids[i], 'state',
                                            rowDatas[i].state + '</br>' +
                                            "<a href=\"javascript:;\" onclick=\"javascript:showRecord('"
                                            + i + "');\">申请驳回</a>");
                                } else if (rejectRecord.acceptedResult == 'NOT_ACCEPTED') {
                                    $("#declarationsDataGrid")
                                        .jqGrid('setCell', ids[i], 'state',
                                            rowDatas[i].state + '</br>' +
                                            "<a href=\"javascript:;\" onclick=\"javascript:showRecord('"
                                            + i + "');\">不同意驳回</a>");
                                } else if (rejectRecord.acceptedResult == 'CHARGEBACK') {
                                    $("#declarationsDataGrid")
                                        .jqGrid('setCell', ids[i], 'state',
                                            "<a href=\"javascript:;\" onclick=\"javascript:showRecord('"
                                            + i + "');\">" + rowDatas[i].state + "</a>");
                                }
                            }
                        }
                    }
                }

            });
        }
        function showRecord(index) {
            if (rejectRecords) {
                if (rejectRecords[index].acceptedResult == 'INITIATED') {
                    layer.alert("驳回原因：" + rejectRecords[index].rejectReason);
                } else if (rejectRecords[index].acceptedResult == 'NOT_ACCEPTED') {
                    layer.alert("不同意原因：" + rejectRecords[index].notAcceptReason +
                        (rejectRecords[index].acceptedOpinion ? '</br>'
                            + "受理意见：" + rejectRecords[index].acceptedOpinion : ''));
                } else if (rejectRecords[index].acceptedResult == 'CHARGEBACK') {
                    layer.alert("退单原因：" + rejectRecords[index].rejectReason);
                }
            }
        }
        /*function doEdit(id, declaration_type,pageType) {
            if (!id) {
                alert("打开页面错误，请与管理员联系！");
                return;
            }
           /!* if (editAble && editAble == true) {
                if (pageType == "CROPPED5") {
                    window.location.href = "../business/agent/order/update?id=" + id;
                }else{
                    window.location.href = "../order/update?id=" + id;
                }
            } else {*!/
                if (pageType == "CROPPED5") {
                    window.location.href = "../business/agent/order/detail?id=" + id;
                }else{
                    window.location.href = "../order/detail?id=" + id;
                }
           // }
        }*/
    </script>
</head>
<body>
<div id="declarationsDataTable"></div>
</body>
</html>
